package com.liuyz;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@SuppressWarnings("all")
public class 泛型 {

    public static void main(String[] args) {

        // List list = new ArrayList();
        List<Employee> list = new ArrayList<>();
        list.add(new Employee("张三", 5500.5, new MyDate(1999, 1, 1)));
        list.add(new Employee("李四", 2500.5, new MyDate(1998, 1, 1)));
        list.add(new Employee("张三", 7500.5, new MyDate(1999, 2, 1)));

        System.out.println(list);

        list.sort(new Comparator<Employee>() {
            @Override
            public int compare(Employee o1, Employee o2) {
                if (o1.getName().equals(o2.getName())) {
                    MyDate o1MyDate = o1.getMyDate();
                    MyDate o2MyDate = o2.getMyDate();
                    return o1MyDate.compareTo(o2MyDate);
                } else {
                    return o1.getName().compareTo(o2.getName());
                }
            }
        });

        System.out.println(list);
    }

}

class Employee {
    private String name;
    private double sal;
    private MyDate myDate;

    public Employee(String name, double sal, MyDate myDate) {
        this.name = name;
        this.sal = sal;
        this.myDate = myDate;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public MyDate getMyDate() {
        return myDate;
    }

    public void setMyDate(MyDate myDate) {
        this.myDate = myDate;
    }

    @Override
    public String toString() {
        return "\nEmployee{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", myDate=" + myDate +
                '}';
    }
}

class MyDate implements Comparable<MyDate> {

    private int year;
    private int month;
    private int day;

    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    @Override
    public String toString() {
        return "MyDate{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }

    @Override
    public int compareTo(MyDate o) {
        if (year != o.getYear()) {
            return year - o.getYear();
        }
        if (month != o.getMonth()) {
            return month - o.getMonth();
        }
        if (day != o.getDay()) {
            return day - o.getDay();
        }
        return 0;
    }
}

